package polep.wespace.utils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import polep.wespace.ssl.HttpConnectionManager;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * HTTP 请求工具类
 *
 * @author : liii
 * @version : 1.0.0
 * @date : 2015/7/21
 * @see : TODO
 */
public class HttpClientUtil {
  private static HttpConnectionManager connManager;
  
  static{
	  connManager = new HttpConnectionManager();
  	  connManager.init();
  }
  public static String doGet(String url){
      CloseableHttpClient httpClient=connManager.getHttpClient();
      HttpGet httpget = new HttpGet(url);
      String json=null;        
      CloseableHttpResponse response=null;
      StringBuffer sb = new StringBuffer();
      try {
          response = httpClient.execute(httpget);
          if(200==response.getStatusLine().getStatusCode()){
        	  InputStream in=response.getEntity().getContent();
              int i = 0;
              byte[] b = new byte[1024];
              while( (i=in.read(b)) != -1){
              	sb.append(new String(b));
              }
              in.close();
          }
          
      } catch (UnsupportedOperationException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }finally {            
          if(response!=null){
          try {
              response.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
          }            
      }                
      return sb.toString();
  }
  
  
  /** 
   * 发送 POST 请求（HTTP），JSON形式 
   * @param apiUrl 
   * @param json json对象 
   * @return 
   */  
  public static String doPost(String apiUrl, String jsonStr) {  
      CloseableHttpClient httpClient=connManager.getHttpClient();
      String httpStr = null;  
      HttpPost httpPost = new HttpPost(apiUrl);  
      CloseableHttpResponse response = null;  

      try {  
          
          StringEntity stringEntity = new StringEntity(jsonStr,"UTF-8");//解决中文乱码问题  
          stringEntity.setContentEncoding("UTF-8");  
          stringEntity.setContentType("application/json");  
          httpPost.setEntity(stringEntity);  
          response = httpClient.execute(httpPost);  
          HttpEntity entity = response.getEntity();  
          System.out.println(response.getStatusLine().getStatusCode());  
          httpStr = EntityUtils.toString(entity, "UTF-8");  
      } catch (IOException e) {  
          e.printStackTrace();  
      } finally {  
          if (response != null) {  
              try {  
                  EntityUtils.consume(response.getEntity());  
              } catch (IOException e) {  
                  e.printStackTrace();  
              }  
          }  
      }  
      return httpStr;  
  }  
}